package net.brokenroad.gamma.server;

import java.io.*;
import java.net.*;

public class ServerLauncher extends Thread
{
	private int port;

	private boolean running;

	public void startServer(int port)
	{
		this.port = port;
		start();
	}

	public void run()
	{
		ServerSocket serverSocket = null;

		try
		{
			serverSocket = new ServerSocket(port);
		} catch (IOException e)
		{
			System.err.println("Could not listen on port: " + port + ".");
			// Need to give the user a useful error message
		}

		while (running)
			try
			{
				new Server(serverSocket.accept(), port).start();
			} catch (IOException e)
			{
				System.err.println("Server died, restarting...");
			}
	}

	public ServerLauncher()
	{
		running = true;
	}

	/**
	 * Begins to shut down the server. New connections are no longer allowed, and
	 * as files are close they may not be reopened.
	 * <p>
	 * The server has the option to immediately disconnect all clients and close
	 * all files, or wait and allow the clients to terminate on their own.
	 * 
	 * @param graceful
	 *          <code>true</code> will wait for clients to disconnect manually,
	 *          <code>false</code> will forcably disconnect them.
	 */
	public void closeServer(boolean graceful)
	{
		running = false;

	}
}
